/**
 * @param {View} shell
 * @constructor
 * @extends BaseMediator
 */
function QuoteMediator(shell)
{
	"use strict";

	this.init(shell, '#quote');

	/** @type {Array.<Quote>} */
	var _list = null;
	/** @type {QuoteMediator} */
	var _this = this;
	/** @type {Number} */
	var _index = 0;
	/** @type {Array.<Number>} */
	var _indices = [];
	/** @type {Quote} */
	var _current = null;

	function init()
	{
		_this
			.addButton('chevron-right', 'Show next quote', _this.show)
			.addButton('book', 'More about the author', more)
			.addButton('chevron-left', 'Show previous quote', function(e) { _this.show(e, true); });
	}

	/**
	 * @param {Event} e
	 * @param {Boolean} [reverse = false]
	 */
	this.show = function(e, reverse)
	{
		if (_list == null)
		{
			$.getScript(Enum.url.quotes, function()
			{
				_list = loadedQuotes;
				for (var i = 0; i < _list.length; i++) { _indices.push(i); }
				Format.shuffle(_indices);
				_this.show();
			});
		}
		else
		{
			if (reverse)
			{
				_index = _index - 2;
				if (_index < 0) { _index = _indices.length - _index; }
			}
			_current = _list[_indices[_index]];
			_this.view.width((_current.text.length > 250) ? 425 : 350);
			_this.content.html(_current.text.replace(/\n/g, '<br/>'));
			_this.bar.html('&mdash; ' + _current.author);
			_this.base.show.call(_this);

			_index++;
			if (_index > _indices.length) { _index = 0; }
		}
	};

	/**
	 * Show more information about the author
	 * @example http://en.wikipedia.org/wiki/Johann_Wolfgang_Von_Goethe
	 */
	function more()
	{
		location.href = Enum.url.wikipedia + _current.author.replace(/\s+/g, '_');
	}

	init();
}
QuoteMediator.inherit(BaseMediator);